// user/order/order.js
import http from '../../http'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    status: 0,
    orderList: []
  },
  gain() {
    http.post({
      url: '/Order/order_list',
      data: {
        user_id: wx.getStorageSync('userId'),
        types: this.data.status
      },
      then: (res) => {
        console.log('订单列表', res);
        this.setData({
          orderList: res.data.data
        })
      }
    })
  },
  onTabChange(event) {
    console.log(event.detail.value);
    this.setData({
      status: event.detail.value
    }, () => {
      if (event.detail.value == 0) {
        http.post({
          url: '/Order/my_order',
          data: {
            user_id: wx.getStorageSync('userId'),
          },
          then: (res) => {
            console.log('获取最新订单', res);
            this.gain()
          }
        })
      } else {
        this.gain()
      }
    })
  },
  takeGoods(e) {
    let info = e.currentTarget.dataset.info
    http.post({
      url: '/Order/confirm_receipt',
      data: {
        id: info.id,
        status: 4
      },
      then: (res) => {
        console.log('确认收货', res);
        if (res.data.code == 1) {
          wx.showToast({
            title: '操作成功',
            icon: 'success',
            duration: 2000
          })
          this.gain()
        }
      }
    })
  },
  orderDetail(e) {
    let info = e.currentTarget.dataset.info
    wx.navigateTo({
      url: `/user/orderDetail/orderDetail?id=${info.id}`,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let options = getCurrentPages()[getCurrentPages().length - 1].options
    this.setData({
      status: options.status
    }, () => {
      this.gain()
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})